/**
 * 
 */
package com.lastproject.web.bean;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.lastproject.dto.UserDTO;
import com.lastproject.ejb.dao.UserDAORemote;

/**
 * @author Alexandra
 *
 */

@ManagedBean
@SessionScoped
public class ChangePasswordBean {
	
	@ManagedProperty(value = "#{userDTO}")
	private UserDTO userDTO;
	
	@EJB
	UserDAORemote userDAO;
	
	private String logUsername = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("logUsername");
	private String logPassword = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("logPassword");

	private String newPassword;
	private String confirmPassword;
	private String errorMsg;
	
	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public ChangePasswordBean() {
		
	}
	
	public ChangePasswordBean(UserDTO userDTO) {
		this.userDTO = userDTO;
	}
	
	public String getUsername() {
		return logUsername;		
	}

	public void setUsername(String username) {
		this.logUsername = username;
	}

	public String getNewPassword() {
		return newPassword; 
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	
	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public UserDTO getUserDTO() {
		return userDTO;
	}

	public void setUserDTO(UserDTO userDTO) {
		this.userDTO = userDTO;
	}
	
	/**
	 * check if passwords provided in GUI are identical 
	 * if return success - call changePassword() 
	 * if return fail - tell them to fuck off
	 * @return success/fail
	 */
	public boolean checkPasswordMatch() {
		
		if(newPassword.equals(confirmPassword)) {
			return true;
		} else {
			return false;
		}
	}

	public String changePassword() {
		
		errorMsg="";
		if(checkPasswordMatch() == true) {
			userDTO = userDAO.changePassword(logUsername, logPassword, newPassword);

			if(userDTO != null) {
				return "login";
			} else {
				errorMsg="Please introduce the correct creditals!";
				return "changePassword";
			}
			
		} else{
			errorMsg="Please introduce the correct creditals!";
			return "changePassword";
		}
	}

}
